

#include <stdio.h>
#include "unistd.h"
#include <errno.h>
#include <signal.h>
#if defined(__WIN32__) || defined(WIN32)
#include <windows.h>

#endif


#include "rtmp.h"
#include "rtmp_packet.h"
#include "amf_packet.h"

void client()
{
    rtmp_packet_t *rtmp_packet;
    amf_packet_t *amf;
    unsigned char output_buffer[1024];
    size_t outputed_size;
    int i;
    unsigned char number_temp[] = {
        0x00, 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    // RTMP
    //printf("=============== RTMP\n");
    unsigned char rtmp_packet_temp[] = {
        0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x14,
        0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x07, 0x63,
        0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x00, 0x3f,
        0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
        0x00, 0x03, 0x61, 0x70, 0x70, 0x02, 0x00, 0x09,
        0x66, 0x61, 0x73, 0x74, 0x70, 0x6c, 0x61, 0x79,
        0x2f, 0x00, 0x08, 0x66, 0x6c, 0x61, 0x73, 0x68,
        0x56, 0x65, 0x72, 0x02, 0x00, 0x0e, 0x57, 0x49,
        0x4e, 0x20, 0x31, 0x30, 0x2c, 0x30, 0x2c, 0x31,
        0x32, 0x2c, 0x33, 0x36, 0x00, 0x06, 0x73, 0x77,
        0x66, 0x55, 0x72, 0x6c, 0x06, 0x00, 0x05, 0x74,
        0x63, 0x55, 0x72, 0x6c, 0x02, 0x00, 0x25, 0x72,
        0x74, 0x6d, 0x70, 0x3a, 0x2f, 0x2f, 0x31, 0x39,
        0x32, 0x2e, 0x31, 0x36, 0x38, 0x2e, 0x31, 0x35,
        0x37, 0x2e, 0x31, 0x32, 0x38, 0x3a, 0x31, 0x39,
        0x33, 0x35, 0x2f, 0x66, 0x61, 0x73, 0x74, 0x70,
        0x6c, 0x61, 0x79, 0x2f, 0x00, 0x04, 0x66, 0x70,
        0x61, 0x64, 0x01, 0x00, 0xc3, 0x00, 0x0c, 0x63,
        0x61, 0x70, 0x61, 0x62, 0x69, 0x6c, 0x69, 0x74,
        0x69, 0x65, 0x73, 0x00, 0x40, 0x2e, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x61, 0x75,
        0x64, 0x69, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x63,
        0x73, 0x00, 0x40, 0xa8, 0xee, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x0b, 0x76, 0x69, 0x64, 0x65,
        0x6f, 0x43, 0x6f, 0x64, 0x65, 0x63, 0x73, 0x00,
        0x40, 0x6f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x0d, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x46,
        0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x00,
        0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x07, 0x70, 0x61, 0x67, 0x65, 0x55, 0x72,
        0x6c, 0x06, 0x00, 0x0e, 0x6f, 0x62, 0x6a, 0x65,
        0x63, 0x74, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69,
        0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x09};

 /*       0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD1, 0x14,
        0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x07, 0x5F,
        0x72, 0x65, 0x73, 0x75, 0x6C, 0x74, 0x00, 0x3F,
        0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
        0x00, 0x06, 0x66, 0x6D, 0x73, 0x56, 0x65, 0x72,
        0x02, 0x00, 0x0D, 0x46, 0x4D, 0x53, 0x2F, 0x33,
        0x2C, 0x30, 0x2C, 0x31, 0x2C, 0x31, 0x32, 0x33,
        0x00, 0x0C, 0x63, 0x61, 0x70, 0x61, 0x62, 0x69,
        0x6C, 0x69, 0x74, 0x69, 0x65, 0x73, 0x00, 0x40,
        0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x09, 0x03, 0x00, 0x05, 0x6C, 0x65, 0x76,
        0x65, 0x6C, 0x02, 0x00, 0x06, 0x73, 0x74, 0x61,
        0x74, 0x75, 0x73, 0x00, 0x04, 0x63, 0x6F, 0x64,
        0x65, 0x02, 0x00, 0x1D, 0x4E, 0x65, 0x74, 0x43,
        0x6F, 0x6E, 0x6E, 0x65, 0x63, 0x74, 0x69, 0x6F,
        0x6E, 0x2E, 0x43, 0x6F, 0x6E, 0x6E, 0x65, 0x63,
        0x74, 0x2E, 0x53, 0x75, 0x63, 0x63, 0x65, 0x73,
        0x73, 0x00, 0x0B, 0x64, 0x65, 0x73, 0x63, 0x72,
        0x69, 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x02, 0x00,
        0x15, 0x43, 0x6F, 0x6E, 0x6E, 0x65, 0x63, 0x74,
        0x69, 0x6F, 0x6E, 0x20, 0x73, 0x75, 0x63, 0x63,
        0x65, 0x65, 0x64, 0x65, 0x64, 0x2E, 0x00, 0x08,
        0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x69, 0x64,
        0x00, 0x41, 0xB2, 0xB1, 0xC0, 0xF3, 0x00, 0x00,
        0x00, 0x00, 0x0E, 0x6F, 0x62, 0x6A, 0x65, 0x63,
        0x74, 0x45, 0x6E, 0x63, 0x6F, 0x64, 0x69, 0x6E,
        0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x09};
		*/

    printf("sample RTMP data size: %d\n", sizeof(rtmp_packet_temp));
    for (i = 0; i < sizeof(rtmp_packet_temp); ++i) {
        printf("%02x, ", rtmp_packet_temp[i]);
    }
    printf("\n");

    rtmp_packet = rtmp_packet_create();
    rtmp_packet_analyze_data(
        rtmp_packet,
        rtmp_packet_temp, sizeof(rtmp_packet_temp),
        AMF_CHANK_SIZE,
        &outputed_size);
    printf("analyzed size: %d\n", outputed_size);
    rtmp_packet_serialize(
        rtmp_packet, output_buffer, 1024, AMF_CHANK_SIZE, &outputed_size);
    printf("serialized size: %d\n", outputed_size);
    for (i = 0; i < outputed_size; ++i) {
        printf("%02x, ", output_buffer[i]);
    }
    printf("\n");

    rtmp_packet_analyze_data(
        rtmp_packet,
        output_buffer, outputed_size,
        AMF_CHANK_SIZE,
        &outputed_size);
    printf("analyzed size: %d\n", outputed_size);
    rtmp_packet_serialize(
        rtmp_packet, output_buffer, 1024, AMF_CHANK_SIZE, &outputed_size);
    printf("serialized size: %d\n", outputed_size);
    for (i = 0; i < outputed_size; ++i) {
        printf("%02x, ", output_buffer[i]);
    }
    printf("\n");

	if(rtmp_packet)
    rtmp_packet_free(rtmp_packet);

    // number
    printf("=============== number for AMF\n");

    amf = amf_packet_analyze_data(
        number_temp, sizeof(number_temp), &outputed_size);
    outputed_size = amf_packet_serialize(
        amf, output_buffer, 1024);
    for (i = 0; i < outputed_size; ++i) {
        printf("%02x, ", output_buffer[i]);
    }
    printf("\n%d\n", outputed_size);
    amf_packet_free(amf);
 /*   
    // boolean
    printf("=============== boolean for AMF\n");
    unsigned char boolean_temp[] = {0x01, 0x01};
    amf = amf_packet_analyze_data(
        boolean_temp, sizeof(boolean_temp), &outputed_size);
    outputed_size = amf_packet_serialize(
        amf, output_buffer, 1024);
    for (i = 0; i < outputed_size; ++i) {
        printf("%02x, ", output_buffer[i]);
    }
    printf("\n%d\n", outputed_size);
    amf_packet_free(amf);
    
    // string
    printf("=============== string for AMF\n");
    unsigned char string_temp[] = {0x02, 0x00, 0x04, 0x69, 0x74, 0x6B, 0x7A};
    amf = amf_packet_analyze_data(
        string_temp, sizeof(string_temp), &outputed_size);
    outputed_size = amf_packet_serialize(
        amf, output_buffer, 1024);
    for (i = 0; i < outputed_size; ++i) {
        printf("%02x, ", output_buffer[i]);
    }
    printf("\n%d\n", outputed_size);
    amf_packet_free(amf);
    
    // object
    printf("=============== object for AMF\n");
    unsigned char object_temp[] = {
        0x03, 0x00, 0x06, 0x66, 0x6D, 0x73, 0x56, 0x65,
        0x72, 0x02, 0x00, 0x0D, 0x46, 0x4D, 0x53, 0x2F,
        0x33, 0x2C, 0x30, 0x2C, 0x31, 0x2C, 0x31, 0x32,
        0x33, 0x00, 0x0C, 0x63, 0x61, 0x70, 0x61, 0x62,
        0x69, 0x6C, 0x69, 0x74, 0x69, 0x65, 0x73, 0x00,
        0x40, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x09};
    amf = amf_packet_analyze_data(
        object_temp, sizeof(object_temp), &outputed_size);
    outputed_size = amf_packet_serialize(
        amf, output_buffer, 1024);
    for (i = 0; i < outputed_size; ++i) {
        printf("%02x, ", output_buffer[i]);
    }
    printf("\n%d\n", outputed_size);
    amf_packet_free(amf);
    
    // null
    printf("=============== null for AMF\n");
    unsigned char null_temp[] = {0x05};
    amf = amf_packet_analyze_data(
        null_temp, sizeof(null_temp), &outputed_size);
    outputed_size = amf_packet_serialize(
        amf, output_buffer, 1024);
    for (i = 0; i < outputed_size; ++i) {
        printf("%02x, ", output_buffer[i]);
    }
    printf("\n%d\n", outputed_size);
    amf_packet_free(amf);
    
    // undefined
    printf("=============== undefined for AMF\n");
    unsigned char undefined_temp[] = {0x06};
    amf = amf_packet_analyze_data(
        undefined_temp, sizeof(undefined_temp), &outputed_size);
    outputed_size = amf_packet_serialize(
        amf, output_buffer, 1024);
    for (i = 0; i < outputed_size; ++i) {
        printf("%02x, ", output_buffer[i]);
    }
    printf("\n%d\n", outputed_size);
    amf_packet_free(amf);

    return 0;


    rtmp_client_t *rc;
    int count;
    rtmp_event_t *event;

    rc = rtmp_client_create("127.0.0.1", 2935);
    if (rc == NULL) {
        printf("failed\n");
        return -1;
    }

    printf("created.\n");

    count = 2000;
    while (count--) {
        rtmp_client_process_message(rc);
        event = rtmp_client_get_event(rc);
        if (event != NULL) {
            if (strcmp(event->code, "NetConnection.Connect.Success") == 0) {
                rtmp_client_create_stream(rc);
                rtmp_client_play(rc);
            }
            rtmp_client_delete_event(rc);
        }
        Sleep(1);
    }

    rtmp_client_free(rc);*/
rtmp_client_create_stream(rc);
rtmp_client_play
}

int main(void)
{
    rtmp_server_t *rs;
	client();

    printf("Hello, RTMP!\n");

    rs = rtmp_server_create(2935);
    if (rs == NULL) {
        printf("failed\n");
        return -1;
    }

    printf("created.\n");

    while (1) {
        rtmp_server_process_message(rs);
//        Sleep(1);
    }

    rtmp_server_free(rs);

    printf("done.\n");

    return 0;
}
